package apps.softtech.fastmarket;

import java.util.ArrayList;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import apps.softtech.Utility.ApplicationConstants;
import apps.softtech.fastmarket.adapter.AdapterChat;
import apps.softtech.fastmarket.adapter.AdapterListChat;
import apps.softtech.fastmarket.model.ChatLog;
import apps.softtech.fastmarket.model.DatabaseHandler;

public class ActivityChatListUser extends Activity{
	/**
	 * id user chat
	 */
	int idUserChat;
	/**
	 * data handler
	 */
	DatabaseHandler dbh;
	/**
	 * list chat id user
	 */
	ListView lvListChatIdUser;
	/**
	 * Title messenger
	 */
	TextView tvTitle;
	/**
	 * list id user
	 */
	ArrayList<ChatLog> mChat_Lists_Id_User = new ArrayList<ChatLog>();
	/**
	 * adapter
	 */
	AdapterListChat mAdapterListChat;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_chat_in_list);
		// init list id
		initGetIdUser();
		//register receiver
		
		tvTitle = (TextView) findViewById(R.id.tvTitleListChat);
		if(mChat_Lists_Id_User.size()==0 || mChat_Lists_Id_User==null){
			tvTitle.setText(getResources().getString(R.string.title_chat_no));
		}
		
		// set list view
		lvListChatIdUser = (ListView)findViewById(R.id.lvListChatItem);
		mAdapterListChat = new AdapterListChat(mChat_Lists_Id_User,ActivityChatListUser.this);
		lvListChatIdUser.setAdapter(mAdapterListChat);
		
		//set onitem click
		lvListChatIdUser.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				idUserChat = mChat_Lists_Id_User.get(position).getFromUserId();
				Intent intent = new Intent(ActivityChatListUser.this,ActivityChat.class);
				intent.putExtra("idfromuser", idUserChat);
				startActivity(intent);
				finish();
			}
		});
	}
	/**
	 * init list id user
	 */
	private void initGetIdUser() {
		dbh = new DatabaseHandler(ActivityChatListUser.this);
		mChat_Lists_Id_User = (ArrayList<ChatLog>) dbh.getChatLog(true,new String[]{ChatLog.FROM_USER_ID,ChatLog.TO_USER_ID}, 
				ChatLog.TO_USER_ID + " =? and " + 
		ChatLog.FROM_USER_ID + " !=?", new String[]{""+ApplicationConstants.MY_USER,""+
				ApplicationConstants.MY_USER}, ChatLog.CHAT_LOG_ID + " DESC", null);
	}
	/**
	 * broadcast receive
	 */
	BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){
		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle exBundle = intent.getExtras();
			int idFromUser = exBundle.getInt("idfromuser");
				if(idFromUser!=idUserChat){
				ChatLog chatLog = new ChatLog();
				chatLog.setFromUserId(idFromUser);
				chatLog.setToUserId(ApplicationConstants.MY_USER);
				if(mChat_Lists_Id_User.size()==0||mChat_Lists_Id_User==null){
					mAdapterListChat = new AdapterListChat(mChat_Lists_Id_User, context);
					lvListChatIdUser.setAdapter(mAdapterListChat);
				}else{
				mChat_Lists_Id_User.add(chatLog);
				mAdapterListChat.upDate(mChat_Lists_Id_User);//
				}
				lvListChatIdUser.setSelection(mChat_Lists_Id_User.size()-1);
				}
		}
		
	};
}
